
<div>
	<span class="label label-info" style="line-height: 25px;">克隆应用功能，是深度克隆一个应用版本所关联的详细信息、icon、snapshots等。</span>
	<span class="label label-info" style="line-height: 25px;">但是，安装包需要上传、版本信息需要重新描述。</span>
	<br /> <br />
	<form id="clone-form" name="clone-version" method="post" action=""
		enctype="multipart/form-data">
		<input type="hidden" name="applicationid" value="{{appMainVo.id}}"
			id="applicationid" /> <input type="hidden" name="id"
			value="{{cloneVo.id}}" id="cloneVersionId" /> <input type="hidden"
			id="cloneBuild" name="build" value="{{cloneVo.build}}">

		<table border="0" cellpaddilistng="0" cellspacing="0"
			class="tab-td-pa" width="100%">
			<tbody>
				<tr>
					<td style="width: 25%;" align="right" for="focusedInput">克隆目标对象：</td>
					<td><span class="label label-info">{{appMainVo.app_name}}</span>
						<span id="cloneVersionText" class="label label-info">{{cloneVo.version}}</span></td>
				</tr>
				<tr>
					<td style="width: 25%;" align="right" for="focusedInput">版本号：</td>
					<td><input type="text" name="newVersion"
						onchange="checkCloneVersion()" id="appNewVersion"
						class="form-control" value="" btvd-type="required" /></td>
				</tr>
				<tr>
					<td align="right" for="focusedInput">构建号：</td>
					<td align="left"><input type="text" name="newBuild"
						onchange="checkCloneBuild()" id="newBuild" class="form-control"
						value="" btvd-type="number" /></td>
				</tr>
				<tr>
					<td style="width: 25%;" align="right">版本描述：</td>
					<td><textarea id="cloneReleaseNote" class="form-control"
							name="releaseNote" btvd-type="required"></textarea></td>
				</tr>
				<tr>
					<td style="width: 30%;" align="right">安装包 ：</td>
					<td>
						<div id="cloneUploadContainer"></div>
					</td>
				</tr>
			</tbody>
		</table>
	</form>

</div>
<script type="text/javascript">
	$('#clone-form').ajaxValidate("submitBut", dataCloneForm);
	var platformSubfix = '{{cloneVo.platformid}}';
	var packageSubfix = {
		"ios" : "ipa",
		"android" : "apk"
	}
	$(function() {
		$('#cloneVersion').on('hidden', function() {
			$('#cloneUploadContainerInput').val('');
			$('#cloneUploadContainerSize').val('');
			$("#cloneUploadContainerName").val('');
		});
		var cloneAndroidUp = buildPlupload({
			id : "cloneUploadContainer",
			elmName : "bundleAndroid",
			filters : [ {
				title : "安装包",
				extensions : packageSubfix[platformSubfix]
			} ],
			tips : "上传的应用安装包不正确,请上传" + packageSubfix[platformSubfix] + "文件",
			targetUrl : "{{ctx}}/mam/attachment/upload",
			defaultVal : "",
			btnName : "上传安装包"
		});

	});

	//提交表单
	function dataCloneForm() {
		if (jQuery("#cloneUploadContainerInput").val() == "") {
			bootbox.alert("请上传" + platformSubfix + "安装包!");
			return false;
		}
		var data = {
			'cloneId' : $("#cloneVersionId").val(),
			'newVersion' : $("#appNewVersion").val(),
			'build' : $("#newBuild").val(),
			'note' : $("#cloneReleaseNote").val()
		};
		$.ajax({
			async : true,
			type : "POST",
			url : "{{ctx}}/mam/appversions/" + platformSubfix + "/clone",
			data : data,
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result == "success") {
					var id = data.cloneObj.id;
					var bundle = $('#cloneUploadContainerInput').val();
					saveBundle(id, bundle);
				} else {
					bootbox.alert(data.msg);
				}
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});

	}

	//进行打包请求
	function saveBundle(versionId, bundle) {
		$.ajax({
			async : true,
			type : "POST",
			url : "{{ctx}}/mam/pkg/bundle/" + versionId + "?bundle=" + bundle,
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result == "success") {
					bootbox.alert("克隆成功！", function() {
						window.location.href = "{{ctx}}/mam/appMain/app-view/"
								+ $("#applicationid").val();
					});
				} else {
					bootbox.alert("克隆失败，请稍后再试！");
				}
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});
	}

	//验证版本号
	function checkCloneVersion() {
		var version = $('#appNewVersion').val();
		var tel = /^\d+\.\d+\.\d+\.\d+|\d+\.\d+\.\d+$/;
		var flag = tel.test(version);
		if (!flag) {
			$('#appNewVersion').attr("btvd-err", "版本号格式不正确，请重新填写。例如：1.0.0");
		} else {
			$('#appNewVersion').removeAttr("btvd-err");
		}
	}

	//验证构建号
	function checkCloneBuild() {
		var build = $('#newBuild').val();
		var oldBuild = "{{newAppVersion.build}}";
		var build = $('#newBuild').val();
		if (parseInt(oldBuild) >= parseInt(build)) {
			$('#newBuild').attr("btvd-err", "此输入项必须为数字并且构建号必须大于" + oldBuild);
		} else {
			$('#newBuild').removeAttr("btvd-err");
		}

	}
</script>


